//
//  GYloginViewController.m
//  GoodLuck
//
//  Created by apple on 2018/1/25.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYloginViewController.h"
#import "GYModifyPasswordViewController.h"
#import "GYRegisterViewController.h"
#import "GYButton.h"
#import "GYNetWorkTools.h"
#import "GYUser.h"
#import "GYDate.h"


@interface GYloginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *UNTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *PWTextFiled;
@property (weak, nonatomic) IBOutlet UIButton *forgetPwBtu;
@property (weak, nonatomic) IBOutlet UIButton *loginBtu;


/** 7天免登入信息 */
@property (nonatomic, strong) GYButton *btu;
@end

@implementation GYloginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //初始化
    [self setupUI];
}

- (void)setupUI {
    self.navigationItem.title = @"登录";
    self.loginBtu.layer.cornerRadius = 4;
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"home_b" tagret:self action:@selector(leftBtuClick) button:nil];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(registerBtuClick)];
    //添加7天自动登入按钮
    GYButton *btu = [GYButton buttonWithType:GYButtonTypeHorizontalImageTitle];
    [btu setImage:[UIImage imageNamed:@"weixuanzhong"] forState:GYControlStateNormal];
    [btu setTitle:@"7天自动登入" forState:GYControlStateNormal];
    [btu setImage:[UIImage imageNamed:@"xuanzhong"] forState:GYControlStateSelected];
    btu.imageSize = CGSizeMake(15, 15);
    btu.selected = YES;
    btu.frame = CGRectMake(20, self.forgetPwBtu.y, 120, 30);
    [btu addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btu];
    self.btu = btu;
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.UNTextFiled resignFirstResponder];
    [self.PWTextFiled resignFirstResponder];
}

- (void)leftBtuClick {
    self.changedViewCallBack();
}

- (void)registerBtuClick {
    GYRegisterViewController *registerVC = [[GYRegisterViewController alloc] initWithNibName:@"GYRegisterViewController" bundle:nil];
    [self.navigationController pushViewController:registerVC animated:YES];
}

#pragma amrk -- 按钮点击事件

- (void)onClick:(GYButton *)sender {
    sender.selected = !sender.selected;
}
//忘记密码
- (IBAction)forgetPwBtuClick {
    GYModifyPasswordViewController *mobVC = [[GYModifyPasswordViewController alloc] initWithNibName:@"GYModifyPasswordViewController" bundle:nil];
    [self.navigationController pushViewController:mobVC animated:YES];
}

//登入按钮
- (IBAction)loginBtuClick {
    
    if (self.PWTextFiled.text.length == 0 || self.UNTextFiled.text.length == 0) {
        //NSLog(@"用户名/密码不能为空...");
        [EasyTextView showText:@"用户名/密码不能为空..."];
    }else {
        [EasyLodingView showLodingText:@"正在登入..."];
        [GYNetWorkTools requestUserLogin:self.UNTextFiled.text password:self.PWTextFiled.text success:^(NSDictionary *obj) {
            //NSLog(@"obj========%@",obj);
            NSString *code = [obj objectForKey:@"code"];
            if ([code integerValue] == 200) {
                [EasyLodingView hidenLoding];
                //1.保存用户信息
                NSDictionary *datas = [obj objectForKey:@"datas"];
                //判断 如果没有点击7天自动登陆 那么每次进来都要登入  点了则7天自动登入
                if (self.btu.selected) {
                    //1.1保存用户信息
                    GYUser *user = [[GYUser alloc] initWithDic:datas];
                    [user saveUserInfo];
                    
                    //2.保存用户登陆时间
                    //[[NSUserDefaults standardUserDefaults] setObject:[GYDate dateTimeConversionTimestamp] forKey:@"userLoginTime"];
                }
                
                //3.跳转界面
                self.loginViewRemoveFromSuperView();
                //[self dismissViewControllerAnimated:YES completion:nil];
            }else {
                [EasyTextView showText:@"用户名/密码错误!"];
                [EasyLodingView hidenLoding];
                NSDictionary *datas = [obj objectForKey:@"datas"];
                NSLog(@"网路加载数据出错===%@",[datas objectForKey:@"error"]);
            }
        } error:^(NSError *error) {
            [EasyTextView showText:@"亲,您的网络可能不太通畅!"];
            [EasyLodingView hidenLoding];
            NSLog(@"loadLoginError=====%@",error);
        }];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.UNTextFiled resignFirstResponder];
    [self.PWTextFiled resignFirstResponder];
}
@end
